// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.commands;

import java.util.Iterator;

import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.ForeignKey;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-9-23 下午01:02:03
 */
public class ForeignKeyReconnectionCommand extends AbstractGEFCommand {

    /** The connection instance to reconnect. */
    private final ForeignKey connection;

    /** The new source endpoint. */
    private DBColumn newSource;

    /** The new target endpoint. */
    private DBColumn newTarget;

    /** The original source endpoint. */
    private final DBColumn oldSource;

    /** The original target endpoint. */
    private final DBColumn oldTarget;

    /**
     * Qiang.Zhang.Adolf@gmail.com ForeignKeyReconnectionCommand constructor comment.
     * 
     * @param fk
     */
    public ForeignKeyReconnectionCommand(ForeignKey fk) {
        this.connection = fk;
        this.oldSource = connection.getSource();
        this.oldTarget = connection.getTarget();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#canExecute()
     */
    public boolean canExecute() {
        if (newSource != null) {
            return checkSourceReconnection();
        } else if (newTarget != null) {
            return checkTargetReconnection();
        }
        return false;
    }

    /**
     * Return true, if reconnecting the connection-instance to newSource is allowed.
     */
    private boolean checkSourceReconnection() {
        // connection endpoints must be different Shapes
        if (newSource.getTable().equals(oldTarget.getTable())) {
            return false;
        }
        if (newSource.equals(oldTarget)) {
            return false;
        }
        // return false, if the connection exists already
        for (Iterator iter = newSource.getOutputs().iterator(); iter.hasNext();) {
            ForeignKey conn = (ForeignKey) iter.next();
            // return false if a newSource -> oldTarget connection exists already
            // and it is a different instance than the connection-field
            if (conn.getTarget().equals(oldTarget) && !conn.equals(connection)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Return true, if reconnecting the connection-instance to newTarget is allowed.
     */
    private boolean checkTargetReconnection() {
        // connection endpoints must be different Shapes
        if (newTarget.getTable().equals(oldSource.getTable())) {
            return false;
        }
        if (newTarget.equals(oldSource)) {
            return false;
        }
        // return false, if the connection exists already
        for (Iterator iter = newTarget.getInputs().iterator(); iter.hasNext();) {
            ForeignKey conn = (ForeignKey) iter.next();
            // return false if a oldSource -> newTarget connection exists already
            // and it is a differenct instance that the connection-field
            if (conn.getSource().equals(oldSource) && !conn.equals(connection)) {
                return false;
            }
        }
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#execute()
     */
    public void execute() {
        if (newSource != null) {
            oldSource.getOutputs().remove(connection);
            oldSource.setForeignKey(false);
            connection.setSource(newSource);
            newSource.setForeignKey(true);
            connection.setTarget(oldTarget);
            newSource.getOutputs().add(connection);
        } else if (newTarget != null) {
            oldTarget.getInputs().remove(connection);
            connection.setSource(oldSource);
            connection.setTarget(newTarget);
            newTarget.getInputs().add(connection);
        }
    }

    /**
     * Getter for newSource.
     * 
     * @return the newSource
     */
    public DBColumn getNewSource() {
        return this.newSource;
    }

    /**
     * Sets the newSource.
     * 
     * @param newSource the newSource to set
     */
    public void setNewSource(DBColumn newSource) {
        this.newSource = newSource;
        setLabel(Messages.getString("ForeignKeyReconnectionCommand.move.startpoint")); //$NON-NLS-1$
        newTarget = null;
    }

    /**
     * Getter for newTarget.
     * 
     * @return the newTarget
     */
    public DBColumn getNewTarget() {
        return this.newTarget;
    }

    /**
     * Sets the newTarget.
     * 
     * @param newTarget the newTarget to set
     */
    public void setNewTarget(DBColumn newTarget) {
        this.newTarget = newTarget;
        setLabel(Messages.getString("ForeignKeyReconnectionCommand.move.endpoint")); //$NON-NLS-1$
        newSource = null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#undo()
     */
    public void undo() {
        connection.setSource(oldSource);
        oldSource.setForeignKey(true);
        connection.setTarget(oldTarget);
    }
}
